class Solution {
    public List<Integer> partitionLabels(String s) {
        // 每个字母最后一次出现的位置
        int[] last = new int[26];
        int n = s.length();

        for (int i = 0; i < n; i++) {
            last[s.charAt(i)-'a'] = i;
        }

        List<Integer> partition = new ArrayList<Integer>();
        int start = 0, end = 0;
        for (int i = 0; i < n; i++) {
            end = Math.max(end, last[s.charAt(i)-'a']);
            if (i == end) {
                partition.add(end-start+1);
                start = end + 1;
            }
        }

        return partition;

    }
}